/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import entities.Teatro;
import entities.Lugar;
import comunicacao.RmiClient;
import comunicacao.RmiClientApp;
import entities.Chair;
import entities.Resposta;
import java.util.ArrayList;
import java.util.Map;
import java.util.Random;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author Leonardo Lino Nº45340
 * @author Diogo Pires Nº40351
 */
@ManagedBean
@RequestScoped
public class SelectedTeatro {

    private String selected;
    private Teatro selectedTeatro;
    private int numRows = 30;
    private int numColumns = 24;
    private ArrayList<Chair> chairs;
    private int estado = 0;
    private int selectedlugar = 0;
    private int clientID = 0;
    
    private Principal principal;

    /**
     * Creates a new instance of SelectedTeatro
     */
    public SelectedTeatro() {
    }

    public void preRender() {

    }

    @PostConstruct
    public void carregou() {

        FacesContext fc = FacesContext.getCurrentInstance();
        selected
                = (String) fc.getExternalContext().
                getRequestParameterMap().get("teatro");

        Resposta resposta = null;
        

        principal = (Principal) FacesContext.getCurrentInstance()
                .getExternalContext().getSessionMap().get("principal");
        clientID = principal.getClientID();
        
        RmiClientApp rc = new RmiClientApp();
        rc.selectorAppServer(principal);

        String lugarParam = (String) fc.getExternalContext().
                getRequestParameterMap().get("lugar");

        String comprar = (String) fc.getExternalContext().
                getRequestParameterMap().get("comprar");

        //comprar
        if (comprar != null && comprar.equals("ok")) {

            selectedlugar = Integer.parseInt(lugarParam);
            resposta = rc.purchaseSeat(selected, selectedlugar, clientID);
            estado = resposta.getEstado();
             selectedTeatro = resposta.getTeatro();
            buildChairs(selectedTeatro);

        } else if (lugarParam == null) {
            resposta = rc.getTeatro(selected);
            selectedTeatro = resposta.getTeatro();
            buildChairs(selectedTeatro);
            //lugar selecionado
        } else {
            selectedlugar = Integer.parseInt(lugarParam);
            System.out.println("select:" + selected + " selectedLugar:" + selectedlugar + " clientID:" + clientID);
            resposta = rc.reserveSeat(selected, selectedlugar, clientID);

            if (resposta == null || resposta.getTeatro() == null || resposta.getTeatro().getLugares() == null) {
                estado = Resposta.ESTADO_BUSY;
            } else {
                selectedTeatro = resposta.getTeatro();
                estado = Resposta.ESTADO_AVAILABLE;
                buildChairs(selectedTeatro);
            }
        }
    }

    private void buildChairs(Teatro teatro) {

        chairs = new ArrayList<Chair>();
        for (Lugar lug : teatro.getLugares()) {
            Chair ch = new Chair();
            ch.setId(lug.id);
            ch.setState(lug.state);

            if (lug.state == Lugar.STATE_FREE) {
                ch.setColorString("green");
                ch.setStateString("free");
            } else if (lug.state == Lugar.STATE_RESERVED) {
                ch.setColorString("yellow");
                ch.setStateString("reserved");
            } else if (lug.state == Lugar.STATE_BOUGHT) {
                ch.setColorString("red");
                ch.setStateString("bought");
            }
            chairs.add(ch);
        }
    }

    public String passedParam() {
        FacesContext fc = FacesContext.getCurrentInstance();
        String str
                = (String) fc.getExternalContext().
                getRequestParameterMap().get("teatro");

        return str;
    }

    public String getSelected() {
        return selected;
    }

    public void setSelected(String selected) {
        this.selected = selected;
    }

    public int getNumRows() {
        return numRows;
    }

    public void setNumRows(int numRows) {
        this.numRows = numRows;
    }

    public int getNumColumns() {
        return numColumns;
    }

    public void setNumColumns(int numColumns) {
        this.numColumns = numColumns;
    }

    public ArrayList<Chair> getChairs() {
        return chairs;
    }

    public void setChairs(ArrayList<Chair> chairs) {
        this.chairs = chairs;
    }

    public int getEstado() {
        return estado;
    }

    public void setEstado(int estado) {
        this.estado = estado;
    }

    public int getSelectedlugar() {

        return selectedlugar;
    }

    public void setSelectedlugar(int selectedlugar) {
        this.selectedlugar = selectedlugar;
    }

    public int getClientID() {
        return clientID;
    }

    public void setClientID(int clientID) {
        this.clientID = clientID;
    }

}
